package io.github.flypiggy.stack.operate.log.spring.boot.starter.configuration.advisor;

import io.github.flypiggy.stack.operate.log.spring.boot.starter.aop.LogAspect;
import io.github.flypiggy.stack.operate.log.spring.boot.starter.configuration.EnableAdvisorConfiguration;
import io.github.flypiggy.stack.operate.log.spring.boot.starter.configuration.LogAspectBase;
import io.github.flypiggy.stack.operate.log.spring.boot.starter.context.OperatorGetter;
import io.github.flypiggy.stack.operate.log.spring.boot.starter.datasource.impl.MongodbRepository;
import io.github.flypiggy.stack.operate.log.spring.boot.starter.properties.OperateLogProperties;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;

@Configuration
@ConditionalOnBean(name = "enableMongo")
@AutoConfigureAfter(EnableAdvisorConfiguration.class)
public class MongoLogAspectConfiguration extends LogAspectBase {

    private final MongoTemplate mongoTemplate;
    private final OperateLogProperties operateLogProperties;

    public MongoLogAspectConfiguration(OperatorGetter operatorGetter,
                                       OperateLogProperties operateLogProperties,
                                       MongoTemplate mongoTemplate) {
        super(operatorGetter, operateLogProperties);
        this.mongoTemplate = mongoTemplate;
        this.operateLogProperties = operateLogProperties;
    }

    @Bean
    public LogAspect mongodbConfigurableLogAspect() {
        return super.logAspectInitializer(new MongodbRepository(mongoTemplate, operateLogProperties.getMongodb()));
    }
}
